package com.ruoyi.web.controller.buttonDescription;

import com.ruoyi.buttonDescription.domain.ButtonDescription;
import com.ruoyi.buttonDescription.service.IButtonDescriptionService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 按钮信息更改Controller
 * 
 * @author ruoyi
 * @date 2023-05-16
 */
@RestController
@RequestMapping("/button/description")
public class ButtonDescriptionController extends BaseController
{
    @Autowired
    private IButtonDescriptionService buttonDescriptionService;

    /**
     * 查询按钮信息更改列表
     */
//    @PreAuthorize("@ss.hasPermi('system:description:list')")
    @GetMapping("/list")
    public TableDataInfo list(ButtonDescription buttonDescription)
    {
        startPage();
        List<ButtonDescription> list = buttonDescriptionService.selectButtonDescriptionList(buttonDescription);
        return getDataTable(list);
    }


    /**
     * 获取按钮信息更改详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:description:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        ButtonDescription buttonDescription = buttonDescriptionService.selectButtonDescriptionById(id);
        if (buttonDescription==null) {
            ButtonDescription defaultButton = new ButtonDescription();
            defaultButton.setButtonName("点击按钮");
            defaultButton.setId(id);
            return success(defaultButton);
        }
        return success(buttonDescription);
    }

    /**
     * 新增按钮信息更改
     */
    @PreAuthorize("@ss.hasPermi('system:description:add')")
    @Log(title = "按钮信息更改", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ButtonDescription buttonDescription)
    {
        System.out.println(buttonDescription);
        if(buttonDescription.getId()==null||buttonDescription.getId()>9||buttonDescription.getId()<=0) {
            return new AjaxResult(400,"请求非法");
        }
        //先判断有没有,没有就插入
        if(buttonDescriptionService.selectButtonDescriptionById(buttonDescription.getId())==null) {
            return toAjax(buttonDescriptionService.insertButtonDescription(buttonDescription));
        }
        return toAjax(buttonDescriptionService.updateButtonDescription(buttonDescription));
    }


    /**
     * noting! 修改按钮信息更改
     */
    @PreAuthorize("@ss.hasPermi('system:description:edit')")
    @Log(title = "按钮信息更改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ButtonDescription buttonDescription)
    {
        System.out.println(buttonDescription);
        if(buttonDescription.getId()==null||buttonDescription.getId()>9||buttonDescription.getId()<=0) {
            return new AjaxResult(400,"请求非法");
        }
        //先判断有没有,没有就插入
        if(buttonDescriptionService.selectButtonDescriptionById(buttonDescription.getId())==null) {
            return toAjax(buttonDescriptionService.insertButtonDescription(buttonDescription));
        }
        return toAjax(buttonDescriptionService.updateButtonDescription(buttonDescription));
    }

    /**
     * 删除按钮信息更改
     */
    @PreAuthorize("@ss.hasPermi('system:description:remove')")
    @Log(title = "按钮信息更改", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(buttonDescriptionService.deleteButtonDescriptionByIds(ids));
    }
}
